package com.bloom.opensourceframework.okhttp.presenter;

import com.bloom.opensourceframework.okhttp.presenter.contact.OkHttpContact;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by bloom on 2018/5/3.
 */

public class OkHttpPresenter implements OkHttpContact.IPresent {

    private OkHttpContact.IView mIView;

    public OkHttpPresenter(OkHttpContact.IView iView) {
        this.mIView = iView;
    }

    @Override
    public void getRequstSyn() {

        /**
         *  同步Get获取
         */
        new Thread(new Runnable() { // 测试先用，实际应用中要用ThreadPool线程池来管理
            @Override
            public void run() {

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525689449594&di=6058c640ef3b399851c9012e83952b0d&imgtype=0&src=http%3A%2F%2Fwww.taopic.com%2Fuploads%2Fallimg%2F140421%2F318743-140421213T910.jpg").build();

                try {
                    Response response = client.newCall(request).execute();

                    if (response.isSuccessful()) {
                        Headers responseHeaders = response.headers();
                        for (int i = 0; i < responseHeaders.size(); i++) {
                            System.out.print(responseHeaders.name(i) + ":" + responseHeaders.value(i));
                        }
                        System.out.print(response.body().string());
                        mIView.synUpdate("get syn request success body:" + response.message());
                    } else {

                        mIView.synUpdate("get syn request fail!");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    mIView.synUpdate("get syn request fail!");
                }
            }
        }).start();
    }

    @Override
    public void getRequstAsyn() {

        /**
         *  异步get请求
         */
        OkHttpClient client2 = new OkHttpClient();
        Request request2 = new Request.Builder().url("Http://publicobject.com/hellowworld.txt").build();

        client2.newCall(request2).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mIView.asynUpdate("get asyn request fail!");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                mIView.asynUpdate("get asyn request success body:" + response.body().string());
            }
        });
    }

    public static final MediaType MEDIA_TYPE_MARKDOWN = MediaType.parse("text/x-markdown;charset=utf-8");

    @Override
    public void postRequst() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                final OkHttpClient client = new OkHttpClient();

                String postBody = ""
                        + "Releases\n"
                        + "--------\n"
                        + "\n"
                        + " * _1.0_ May 6, 2013\n"
                        + " * _1.1_ June 15, 2013\n"
                        + " * _1.2_ August 11, 2013\n";

                Request request = new Request.Builder()
                        .url("https://api.github.com/markdown/raw")
                        .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, postBody))
                        .build();

                try {
                    Response response = client.newCall(request).execute();
                    if (response.isSuccessful()) {

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
